﻿@model GsifWeb4.ViewModels.MatchReportViewModel

<script type="text/javascript">

    // Enabla/Disable textrutan för Omgång beroende på om det är seriematch eller annan match som blir vald
    $(document).ready(function () {
        $("#MatchTypeDD").change(enableRoundTextBox);
    });

    // Kollar värdet på vald matchtyp och enable/disable textrutan Omgång
    function enableRoundTextBox() {
        var selectedText = "";
        selectedText += $(this).val() + " ";
        if ('' + selectedText.toString().toLowerCase().trim() == 'seriematch') {
            $("#RoundTextBox").removeAttr('disabled');
        }
        else {
            $("#RoundTextBox").attr('disabled', 'disabled');
        }
    }

    // Lägg till lyssnare på när en dropdown med spelare ändras
    $(document).ready(function () {
        $('select[name^="Player_"]').change(fillListForManOfTheMatch);
    });

    // Bygger upp listan med spelare för matchens lirare
    function fillListForManOfTheMatch() {
        $("#ManOfTheMatch").get(0).options.length = 0;
        $("#ManOfTheMatch").get(0).options[0] = new Option("<-- Välj -->", "-1");

        $('select[name^="Player_"]').each(function () {
            if ($(this).val() > 0) {
                $("#ManOfTheMatch").get(0).options[$("#ManOfTheMatch").get(0).options.length] = new Option($(this).find("option:selected").text(), $(this).val());
            }
        });
    }

    // Kontrollera att alla startspelarpositioner är ifyllda
    function checkThatStartPlayersAndManOfTheMatch() {

        var choosenPlayers = new Array();
        var cancelSave = false;

        // Kontrollera att ingen spelare är satt på fler än en position
        $(".players").find('select[name^="Player_"]').each(function () {

            // Skippa positioner där ingen spelare är vald (utan endast tex <- Försvarare ->)            
            var playerId = $(this).val();
            if (playerId < 0)
                return true; // continue;

            if (choosenPlayers.indexOf(playerId) < 0) {
                choosenPlayers.push(playerId);
            }
            else {
                alert($(this).find("option:selected").text() + ' finns på två positioner. Matchreferat kunde ej sparas.');
                event.preventDefault();
                cancelSave = true;
                return;
            }
        });
        if (cancelSave)
            return;

        $("div#StartingPlayers").find('select[name^="Player_"]').each(function () {

            // Kontrollfråga om inte alla startspelarpositioner är ifyllda
            if ($(this).val() <= 0) {
                if (!confirm('På minst en position finns ingen spelare vald. Vill du fortsätta ändå?')) {
                    event.preventDefault();
                    cancelSave = true;
                    return;
                }
            }
        });
        if (cancelSave)
            return;

        // Kontrollfråga om inte matchesn lirare vald
        if ($("#ManOfTheMatch").val() < 0) {
            if (!confirm('Ingen matchens lirare är vald. Vill du fortsätta ändå?')) {
                event.preventDefault();
                return;
            }
        }
    }

    $(document).ready(function () {
        $("input[name=saveMatchReport]").click(checkThatStartPlayersAndManOfTheMatch);
    });

    $(".numericwidth20").keyup(function () {
        this.value = this.value.replace(/[^0-9\.]/g, '');
    });

</script>


<div class="EditMatchReportResult">    
    @{
        string homeTeam = Model.Match.HomeGame ? "GSIF" : Model.Match.Opponent;
        string awayTeam = Model.Match.HomeGame ? Model.Match.Opponent : "GSIF";
        string gsifImage = "<img src='../../Images/SiteImages/logotype_43x45.jpg' width='43' height='45'/>";
        string opponentImg = "<img src='../../Images/SiteImages/fotboll_40x40.jpg' width='43' height='45'/>";
        string homeTeamImg = Model.Match.HomeGame ? gsifImage : opponentImg;
        string awayTeamImg = Model.Match.HomeGame ? opponentImg : gsifImage;         
    }
    <h2>
        @Html.Raw(homeTeamImg) @homeTeam - @awayTeam @Html.Raw(awayTeamImg)
    </h2>

</div>
<div>
    @using (Html.BeginForm())
    {
        <div>
            @Html.ValidationSummary(true)

            <div class="editor-label">
                @GsifWeb4.ExtensionMethods.Label("Mål vid full tid:", new { @class = "fatLabel" })
            </div>
            <div class="editor-field">
                GSIF: 
                @Html.TextBoxFor(model => model.Match.GoalsFullTimeGSIF, new { @class = "numericwidth20" })
                @Html.ValidationMessageFor(model => model.Match.GoalsFullTimeGSIF)
                @Model.Match.Opponent:
                @Html.TextBoxFor(model => model.Match.GoalsFullTimeOpponent, new { @class = "numericwidth20" })
                @Html.ValidationMessageFor(model => model.Match.GoalsFullTimeOpponent)
            </div>
            <div class="editor-label">
                @GsifWeb4.ExtensionMethods.Label("Mål i halvlek:", new { @class = "fatLabel" })
            </div>
            <div class="editor-field">
                GSIF: 
                @Html.TextBoxFor(model => model.Match.GoalsHalfTimeGSIF, new { @class = "numericwidth20" })
                @Html.ValidationMessageFor(model => model.Match.GoalsHalfTimeGSIF)
                @Model.Match.Opponent:
                @Html.TextBoxFor(model => model.Match.GoalsHalfTimeOpponent, new { @class = "numericwidth20" })
                @Html.ValidationMessageFor(model => model.Match.GoalsHalfTimeOpponent)
            </div>
            <div class="editor-label">
                @GsifWeb4.ExtensionMethods.Label("Walk over:", new { @class = "fatLabel" })
                @Html.CheckBoxFor(model => model.Match.WalkOver)
            </div>
            <div class="editor-label">
                @GsifWeb4.ExtensionMethods.Label("Typ av match:", new { @class = "fatLabel" })
                @Html.DropDownList("MatchTypeDD", Model.Match.MatchType, new { id = "MatchTypeDD" })
                @GsifWeb4.ExtensionMethods.Label("Omg:", new { @class = "fatLabel" })
                @Html.TextBoxFor(model => model.Match.Round, new { id = "RoundTextBox", @class = "numericwidth20"})
            </div>
        </div>
    
        <hr />
        <div id="StartingPlayers" class="players">
            @*<table border="0">
                <!-- Nedan kommer planen med alla spelarna -->
                <tr>
                    <td colspan="3">*@
                        <table style="border: 1px solid black; background-image: url('../../Images/SiteImages/planhalva_302.jpg'); background-repeat: no-repeat; background-position: center; background-size:100% 100%;">
                            <tr>
                                <!-- Anfallare -->
                                @{Html.RenderPartial("_PlayersAtStartPositionForm", Model.MatchStatistics, new ViewDataDictionary { { "minPosition", 40 }, { "maxPosition", 50 } });}
                            </tr>
                            <tr>
                                <!-- Mittfältare -->
                                @{Html.RenderPartial("_PlayersAtStartPositionForm", Model.MatchStatistics, new ViewDataDictionary { { "minPosition", 30 }, { "maxPosition", 40 } });}
                            </tr>
                            <tr>
                                <!-- Försvarare -->
                                @{Html.RenderPartial("_PlayersAtStartPositionForm", Model.MatchStatistics, new ViewDataDictionary { { "minPosition", 20 }, { "maxPosition", 30 } });}
                            </tr>
                            <tr>
                                <!-- Målvakt -->
                                @{Html.RenderPartial("_PlayersAtStartPositionForm", Model.MatchStatistics, new ViewDataDictionary { { "minPosition", 10 }, { "maxPosition", 20 } });}
                            </tr>
                        </table>
            @*        </td>
                </tr>
            </table>*@
        </div>
        <!-- Avbytare -->
        <div class="players alignLeft defaultMargin ">
            @{Html.RenderPartial("_PlayersAtTheBenchForm", Model.MatchStatistics, new ViewDataDictionary { { "minPosition", 50 }, { "maxPosition", 60 } });}
        </div>

        <!-- Matchens lirare -->
        <div class="alignLeft defaultMargin">
            @GsifWeb4.ExtensionMethods.Label("Matchens lirare:", new { @class = "fatLabel" })
            @Html.DropDownList("ManOfTheMatch", Model.ManOfMatchPlayers)
        </div>

        <!-- Matchrapporten -->
        <div class="alignLeft defaultMargin">
            @GsifWeb4.ExtensionMethods.Label("Matchreferat:", new { @class = "fatLabel" })
            <br />
            @*@Html.TextBoxFor(model => model.Match.MatchReport);*@

            <script type="text/javascript">
                tinyMCE.init({
                    theme: "advanced",
                    mode: "textareas",
                    plugins: "fullpage",
                    theme_advanced_buttons3_add: "fullpage"
                });
        </script>
            <div class="editor-label">
            @Html.LabelFor(model => model.Match.MatchReport)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Match.MatchReport)
            @Html.ValidationMessageFor(model => model.Match.MatchReport)
        </div>


        </div>
    
        <div>
            <p>
                <input name="saveMatchReport" type="submit" value="Spara matchreferat" />
            </p>

        </div>
        <!-- Slutet på form -->
    }
</div>

@*        <% Html.Telerik().EditorFor(model => Model.Match.MatchReport)
           .HtmlAttributes(new { style = "height:300 px" })
           .FileBrowser(t => t.Browse("Browse", "ImageBrowser")
               .Thumbnail("Thumbnail", "ImageBrowser")
               .Upload("Upload", "ImageBrowser")
               .DeleteFile("DeleteFile", "ImageBrowser")
               .DeleteDirectory("DeleteDirectory", "ImageBrowser")
               .CreateDirectory("CreateDirectory", "ImageBrowser"))
               .Render(); %>*@